package com.example.framework;

import org.openqa.selenium.By;

import com.example.framework.dataobjects.GroupObject;

public class GroupHelper  extends FormHelper{

	public GroupHelper(ApplicationManager manager) {
		super(manager);

	}

	public void createGroup(GroupObject groupCredentials) {
		manager.getNavigationHelper().goToGroupListPage();
		createNewGroup(groupCredentials);
		returnToGroupListPage();
	}

	protected void createNewGroup(GroupObject groupCredentials) {
		click(By.name("new"));
		populateForm(groupCredentials);
		submitGroupCreation();	
	}

	private void submitGroupCreation() {
		clickOnButton("submit");
	}

	protected void populateForm(
			GroupObject groupCredentialsObject) {
		
		String groupNameLocator = "group_name";
		String groupHeaderLocator = "group_header";
		String groupFooterLocator = "group_footer";

		type(By.name(groupNameLocator), groupCredentialsObject.groupName);
		type(By.name(groupHeaderLocator), groupCredentialsObject.groupHeader);
		type(By.name(groupFooterLocator), groupCredentialsObject.groupFooter);
	}

	protected void returnToGroupListPage() {
		click(By.linkText("group page"));
	}

	public void deleteGroup(int groupId) {
		manager.getNavigationHelper().goToGroupListPage();
		checkGroup(groupId);
		clickOnButton("delete");
		returnToGroupListPage();

	}
	
	public void modifyGroup(int groupId, GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupModification(groupId);
		populateForm(groupObject);
		submitGroupModification();
		returnToGroupListPage();

	}

	private void submitGroupModification() {
		clickOnButton("update");	
	}

	private void initGroupModification(int groupId) {
		checkGroup(groupId);
		click(By.name("edit"));
	}

	private void checkGroup(int i) {
		click(By.name("selected[]"));
	}
	
	

}
